package com.my.java.mq.rabbitmq.topic;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeoutException;

import com.my.java.mq.rabbitmq.PointToPoint;
import com.rabbitmq.client.Channel;  
//发送消息端  
public class SendLogTopic extends PointToPoint{  
    
  public SendLogTopic(String pointName) throws IOException, TimeoutException {
        super(pointName);
    }
  
private static final String EXCHANGE_NAME = "topic_logs";  

  public static void main(String[] args) throws Exception {  
      SendLogTopic sendLogTopic = new SendLogTopic("SendLogTopic");

      Channel channel = sendLogTopic.getChannel("topic");
      // 声明转发器  
      channel.exchangeDeclare(EXCHANGE_NAME, "direct",true);    
      //定义绑定键     
      String[] routing_keys = new String[] { "kernal.info", "cron.warning",    
              "auth.info", "kernel.critical" };    
      int i = 10000;
      while(i-- > 0){
          for (String routing_key : routing_keys)    
          {     
              //发送4条不同绑定键的消息  
              String msg = UUID.randomUUID().toString();    
              channel.basicPublish(EXCHANGE_NAME, routing_key, null, msg    
                      .getBytes());    
              System.out.println(" [x] Sent routingKey = "+routing_key+" ,msg = " + msg + ".");    
          }    
      }
      sendLogTopic.close();
  }  

}  
